<?php declare(strict_types=1);

namespace app\api\service;

use app\api\model\Crop;
use app\api\model\CropCategory;
use app\api\model\Land;
use app\api\model\Personnel;
use app\api\service\traits\Oprations;

class CropService
{
    use Oprations;

    private $relations = 
    [
        Land::class => 'land_id',
        Personnel::class => 'personnel_id',
        CropCategory::class => 'variety'
    ];

    public function __construct()
    {
        $this->model = new Crop();
    }

    /**
     * 批量添加
     *
     * @return void
     */
    public function addAll()
    {
        // 获取参数
        $argList = func_get_args();
        // 查询条件
        $data = isset($argList[0]) ? $argList[0]['rows'] : [];
        // 是否打印SQL
        $is_sql = isset($argList[1]) ? $argList[1] : false;
        if (!$data) {
            $data = request()->param('rows', null);
        }
        $error = '';
        $ids = [];
        if (null != $data) {
            foreach ($data as $row) {
                $rowId = $this->model->edit($row, $error, $is_sql);
                if (!$rowId) {
                    return message($error, false);
                }
                else {
                    $ids[] = $rowId;
                }
            }
            if (!empty($ids)) 
            {
                return message();
            }
        }
        return message($error, false);
    }
}